package com.bike.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.Request.Builder;

public class OkHttpRequestUtil {
	public static String doGet(String url,Map<String, String> headers){
		Builder builder=new Request.Builder();
     	if(headers!=null){
     		for(String key:headers.keySet()){
     			builder.addHeader(key, headers.get(key));
     		}
     	}
		
        //创建一个Request
        final Request request = builder
                .url(url)
                .build();
        return  execute(request);
    }
    public static String doPost(String url,String reqbody,Map<String,Object> map){

        FormEncodingBuilder builder=addParamToBuilder(reqbody, map);
         RequestBody body = builder.build();
          Request request = new Request.Builder()
          .url(url)
          .post(body)
          .build();
        return execute(request);
    }
    public static String doPost(String url,String reqbody,Map<String,Object> map,Map<String,String> headers){

        FormEncodingBuilder bodyBuilder=addParamToBuilder(reqbody, map);
         RequestBody body = bodyBuilder.build();
     	Builder builder=new Request.Builder();
     	if(headers!=null){
     		for(String key:headers.keySet()){
     			builder.addHeader(key, headers.get(key));
     		}
     	}
          Request request = builder
          .url(url)
          .post(body)
          .build();
        return execute(request);
    }
    public static String doPut(String url,String reqbody,Map<String,Object> map){
        FormEncodingBuilder builder=addParamToBuilder(reqbody, map);
         RequestBody body = builder.build();
          Request request = new Request.Builder()
          .url(url)
          .put(body)
          .build();
        return execute(request);
    }
    public static String doDelete(String url,String reqbody,Map<String,Object> map){
        FormEncodingBuilder builder=addParamToBuilder(reqbody, map);
        RequestBody body = builder.build();
        Request request = new Request.Builder()
                .url(url)
                .delete(body)
                .build();
        return execute(request);
    }
    public static FormEncodingBuilder  addParamToBuilder(String reqbody,Map<String,Object> map){
        FormEncodingBuilder builder=new FormEncodingBuilder();
        if(!StringUtils.isEmpty(reqbody)){
            if(reqbody.startsWith("?")){
                reqbody=reqbody.substring(1);
            }
            String[] params=reqbody.split("&");
            for(int i=0;i<params.length;i++){
                if(params[i].equals("")){
                    continue;
                }
                String [] kv=params[i].split("=");
                builder.add(kv[0], kv[1]);
            }
        }
        if(map!=null){
            Iterator<Entry<String,Object>> ite= map.entrySet().iterator();
            for(;ite.hasNext();){
                Entry<String,Object> kv=ite.next();
                builder.add(kv.getKey(), kv.getValue().toString());
            }
        }
        return builder;
    }

     public static String execute(Request request){
         try {
             OkHttpClient client = new OkHttpClient();
             Response response = client.newCall(request).execute();
            if (response.isSuccessful()) {
                return response.body().string();
            } 
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "{\"error\":\"fail\"}";
     }
     public void pug(){
    	 
     }
     public static void main(String[] args) {
    	 	Map<String, String>get = new HashMap<>();
			get.put("openId", "www.teyuntong.cn");
    	String result=doGet("http://jx.1000cb.com/mchat/public/teacher_lessonwork.html",get);
    	 System.out.println(result);
    	// 5586580375
     }
}
